package com.samp.solr.file2solr;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.CommonParams;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

public class ExtractAllIntention {

    private static List<String> resultList = new ArrayList<>();

    private static final String BASE_SOLR_URL = "http://localhost:8080/solr/gcc-omcp";

    private static void extractIntention(String rawPath) throws Exception{
        Path filePath = Paths.get(rawPath);
        BufferedReader bufferReader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8);
        String line;
        try {
            bufferReader.readLine();
            Set<String> set = new HashSet<>();
            while( (line = bufferReader.readLine()) != null ){
                String semantic =line;

                //通过意图，新增一条语料
                String newWords = ErrorIntentionExtract.intentionToWord(semantic);
                if( StringUtils.isNotBlank(newWords) && !set.contains(newWords.trim()+"|"+semantic.trim())){
                    set.add(newWords.trim()+"|"+semantic.trim());
                    resultList.add(newWords.trim()+"\t"+ErrorIntentionExtract.removeLastSlot(semantic)+"\t1");
                }

            }
            System.out.println("data process finished.");
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            bufferReader.close();
        }
        System.out.println("all done!");
    }

    private static void analyzeIntention(String rawPath) throws Exception {
        Path filePath = Paths.get(rawPath);
        BufferedReader bufferReader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8);
        String line;
        try {
            SolrClient solrClient =new HttpSolrClient.Builder(BASE_SOLR_URL).build();
            Set<String> list = new HashSet<>();
            while( (line = bufferReader.readLine()) != null ){
                String[] splits = line.split("\t");
                if( splits.length == 3 ){
                    if( !list.contains(splits[1]) ) {
                        list.addAll(getIntentionWords(splits[1]));
                    }
                }
            }
            for(String s: list ){
//                System.out.println(s);
                if( StringUtils.isNotBlank(s) && s.length() > 1 ) {
                    solrQuery(solrClient, s);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            bufferReader.close();
        }
        System.out.println("all done!");
    }

    private static Set<String> getIntentionWords(String rawIntention){
        Set<String> set = new HashSet<>();
        String[] intentionSplits = rawIntention.trim().split("#");
        for( int i = 0 ;i < intentionSplits.length ; i++ ){
            String intention = intentionSplits[i];
            String[] details = intention.split("=");
//            System.out.println(rawIntention);
            if( details.length < 2 ){
                break;
            }
            if(  !"default".equals(details[1]) && !"execute".equals(details[1])
                    && !"chat".equals(details[1]) && !"集外".equals(details[1]) ){
                String[] split = ErrorIntentionExtract.tokenize(details[1]).split(" ");
                for(String s : split){
                    set.add(s);
                }
            }
        }
        return set;
    }

    private static void solrQuery(SolrClient solrClient, String words){
        SolrQuery query = new SolrQuery();
        query.set(CommonParams.QT, "/select");
        query.set(CommonParams.START, "0");
        query.set(CommonParams.ROWS, 10);
        String queryParam = "content:"+words;
        query.set(CommonParams.Q, queryParam);
        query.set(CommonParams.FL, "content,score");
        try {
            QueryResponse response = solrClient.query(query);
            Long resultNum = response.getResults().getNumFound();
            System.out.println("words:"+words+", num:"+resultNum);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void analyzeErrorMatch(String rawPath) throws Exception {
        Path filePath = Paths.get(rawPath);
        BufferedReader bufferReader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8);
        String testLine = null;
        try {
            List<String> noAnalyzeList = new ArrayList<>();
            List<String> otherList = new ArrayList<>();
            while( (testLine = bufferReader.readLine() ) != null ){
                String solrLine = bufferReader.readLine();
                if( isNoNeedAnalyze(testLine, solrLine) ){
                    noAnalyzeList.add(testLine);
                    noAnalyzeList.add(solrLine);
                }else {
                    otherList.add(testLine);
                    otherList.add(solrLine);
                }
            }
            System.out.println("no analyze rate = "+(noAnalyzeList.size()*1.0/(noAnalyzeList.size()+otherList.size())));
//            for( String s : otherList ){
//                System.out.println(s);
//            }
            ErrorIntentionExtract.saveResultToFile(otherList, "D:\\gcc-omcp\\new\\data\\test\\018_test_add_intentions_others.txt");
            System.out.println("|||||||||||||||||||||||||||||||||||||||||||||||||");
            for( String s : noAnalyzeList ){
                System.out.println(s);
            }

//            Collections.sort(list);
//            for(String s: list ){
//                System.out.println(s);
//            }
        }catch (Exception e) {
            System.out.println(testLine);
            e.printStackTrace();
        }finally{
            bufferReader.close();
        }
        System.out.println("all done!");
    }

    private static boolean isNoNeedAnalyze(String testLine, String solrLine){
        //如果全匹配，就不用再分析了。
        String[] test = testLine.split("\t");
        String[] solr = solrLine.trim().split("\t");
        String testWords = test[0].trim();
        String solrWords = solr[0].trim().substring(2);
        if( testWords.equals(solrWords) ){
            return true;
        }
        //如果测试数据是集外和char，也暂时不分析
        Set<String> intentions = new HashSet<>();
        intentions.add("action.name=default#target.name=集外#target.params1=default");
        intentions.add("action.name=default#target.name=chat");
        if( intentions.contains(test[1].trim()) ){
            return true;
        }
        return false;
    }

    /**
     * 分析第四个槽位
     * @param rawPath
     * @throws Exception
     */
    private static void analyzeLastSolt(String rawPath, String resultFile) throws Exception{
        Path filePath = Paths.get(rawPath);
        BufferedReader bufferReader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8);
        String line;
        try {
            Integer index = 0;
            SortedMap<String,List<String>> sortedMap = new TreeMap();
            Set<String> lastSoltSet = new HashSet<>();
            while( (line = bufferReader.readLine()) != null ){
                index++;
                String[] splits = line.split("#");
                if( splits.length < 4 ){
                    System.out.println("error data:"+line+",index="+index);
                    continue;
                }

                //如果大于4，只取有四个
                if( splits.length > 4 ){
                    String[] newSplits = new String[4];
                    for(int i = 0  ; i < 4 ; i++ ){
                        newSplits[i]=splits[i];
                    }
                    splits=newSplits;
                }
                String key = normText(splits[0]+"#"+splits[1]+"#"+splits[2]).trim();
                if( sortedMap.get(key) == null){
                    sortedMap.put(key,new ArrayList<>());
                }
                String[] lastSoltSplits = splits[3].split("=");
                if( lastSoltSplits.length != 2 ){
                    System.out.println("error error data:"+line);
                    continue;
                }
                String lastSoltName = lastSoltSplits[1];
                if( "*".equals(lastSoltName) ){
                    lastSoltName = "default";
                }
                lastSoltSet.add(lastSoltName.trim());
                if( !sortedMap.get(key).contains(lastSoltName) ) {
                    sortedMap.get(key).add(lastSoltName);
                }
            }
            Iterator iterator = sortedMap.entrySet().iterator();
            List<String> resultList = new ArrayList<>();
            while (iterator.hasNext()){
                Map.Entry entry = (Map.Entry) iterator.next();
                String key = (String)entry.getKey();
                List<String> data = (List)entry.getValue();
//                System.out.println(key+"|"+StringUtils.join(data,";"));
                resultList.add(key+"|"+StringUtils.join(data,";"));
            }
            ErrorIntentionExtract.saveResultToFile(resultList, resultFile);
            System.out.println("===============>");
            System.out.println(lastSoltSet);
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            bufferReader.close();
        }
    }

    /**
     * 将*，execute统一替换成default
     * @param word
     * @return
     */
    private static String normText(String word){
        word = word.replaceAll("\\*","default");
        word = word.replaceAll("execute","default");
        return word;
    }

    private static void removeRepeatedRecord(String rawPath,String outFile) throws Exception {
        List<String> list = new ArrayList<>();
        Path filePath = Paths.get(rawPath);
        BufferedReader bufferReader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8);
        String line;
        try {
            while( (line = bufferReader.readLine() ) != null ){
                String intention = normText(line.trim());
                if( !list.contains(intention) ){
                    list.add(intention);
                }
            }
            ErrorIntentionExtract.saveResultToFile(list,outFile);
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            bufferReader.close();
        }
    }

    public static void main(String[] args) {
        ErrorIntentionExtract.loCustomDict();
        ErrorIntentionExtract.loadStopWords(true);
        String rawPath = "D:\\gcc-omcp\\all_intention.txt";
//        rawPath = "D:\\gcc-omcp\\all_intention_from_data.txt";
        String distPath ;
        String errorPath = "D:\\gcc-omcp\\new\\data\\test\\018_test_data_no_dul_no_match_add_intentions.txt";
        try{
//            extractIntention(rawPath);
//            ErrorIntentionExtract.saveResultToFile(resultList, distPath);
//            ErrorIntentionExtract.checkFile(distPath);
//            analyzeIntention(distPath);
//            analyzeErrorMatch(errorPath);
            distPath =  "D:\\gcc-omcp\\all_intention_with_last_slot.txt";
//            analyzeLastSolt(rawPath,distPath);
            distPath =  "D:\\gcc-omcp\\all_intention_remove_repeat.txt";
            removeRepeatedRecord(rawPath,distPath);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
